package com.elite.groovy.callback
import com.elite.groovy.util.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.elite.groovy.util.Util;
import net.sf.json.JSONObject;
/**
 * @author luper
 *
 */
public class PinAnReportHFCreate implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(PinAnReportHFCreate.class);
	private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 0;	
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {


		def updatesqlresult2,isvip;

		/**
		 * 平安创展回访
		 */


		log.debug("平安创展回访数据开始生成！");

		eliteBean.update("insert into obt_bus_list (BUS_ID,BATCH,BUS_TYPE,POLICYNUM,STR_K,STR_M,STR_V,STR_W,STR_Z,STR_AA,STR_AF,STR_AG,STR_A) select SEQOBTBUSLIST.nextval,to_char(sysdate,'yyyymmddhh24miss')||'DDP','DDP',dr.policy_no,dr.license_no,dr.clm_no,dr.insured_name,dr.contect_number,dr.reportor_name,dr.reportor_phone,dr.linker_name,dr.linker_phone,'' from dh_report dr  where  substr(dr.policy_no,1,1)='B' and dr.report_time >=substr(to_char(sysdate -1/144,'yyyy-mm-dd hh24:mi'),1,15)||'0:00'  and dr.report_time <substr(to_char(sysdate,'yyyy-mm-dd hh24:mi'),1,15)||'0:00' ","dataSource");

		log.debug("平安创展未回访数据提前打标");
		def updatesql1="update OBT_BUS_LIST set TASKSTATUS=3 where TASKSTATUS=0 and bus_type ='DDP' and to_date(substr(batch,1,14),'yyyy-mm-dd hh24:mi:ss')<= to_date(to_char(sysdate -1/24,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') ";
		def updatesqlresult1=eliteBean.update(updatesql1,"dataSource");
		log.debug("updatesqlresult1:"+updatesqlresult1);

		log.debug("平安创展只生成人工回访");
		log.debug("平安创展报案插入中间表");

		def relist=eliteBean.chaxun_sql("select sysguid as vguid,sysguid as oguid, str_M ,0 as flag,'REPORT' as obtype,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as curdate,'','LH1R7R' as mapid,'DDP' as btype,LINKER_PHONE,linker_name,r.policy_no from OBT_BUS_LIST l left join DH_REPORT r on r.CLM_NO=l.str_M where bus_type ='DDP' and to_date(substr(batch,1,14),'yyyy-mm-dd hh24:mi:ss') <=to_char(sysdate -1/24,'yyyy-mm-dd hh24:mi:ss') and TASKSTATUS=3 ","dataSource");

		if(relist.size()>0){
			for(def j=0;j<relist.size();j++){
				isvip=doselete(relist[j].get("policy_no"),relist[j].get("LINKER_PHONE"),eliteBean,relist[j].get("linker_name"),charset,timeout,enableSSLflag);

				if(isvip=="2"){
					//普通客户直接生成回访数据
					updatesqlresult2=eliteBean.update("insert into Ob_visitTask(VISITTASK_GUID,OBJECTIVE_GUID,BUSINESSNO,TASKSTATUS,BUSTYPE, CREATEDDATE, CREATEDBY,MAP_ID,BUSTYPE_REPORT,CALLLOSE)values('"+relist[j].get("vguid")+"','"+relist[j].get("oguid")+"','"+relist[j].get("str_M")+"',1,'"+relist[j].get("obtype")+"',to_date('"+relist[j].get("curdate")+"','yyyy-mm-dd hh24:mi:ss'),'','"+relist[j].get("mapid")+"','"+relist[j].get("btype")+"',1)","dataSource")
					def custid=eliteBean.chaxun_sql("select customer_guid from(select customer_guid from CUSTOMER c  where mobile= '"+relist[j].get("LINKER_PHONE")+"' and stringfield18 = '"+relist[j].get("policy_no")+"' order by MODIFIEDDATE desc) where rownum=1","dataSource")
					if(custid.size()>0){
						def listtemp=custid[0]
						isvip = listtemp.get("customer_guid")
					}else{
						def cguid=Util.getGuid()
						eliteBean.update("insert into customer(CUSTOMER_GUID,customertype_id,CUSTOMERNAME,MOBILE,stringfield18,createddate,createdby,MODIFIEDDATE,MODIFIEDBY)values('"+cguid+"','KFjoin','"+relist[j].get("linker_name")+"','"+relist[j].get("LINKER_PHONE")+"','"+relist[j].get("policy_no")+"',sysdate,'SELITE',sysdate,'SELITE')","dataSource")
						isvip = cguid
					}
					//生成 objective
					def updatesql45="insert into objective (OBJECTIVE_GUID, OBJECTIVETYPE_ID, HANDLEGROUP_ID,PRIORITY_ID,EXPIREDDATE,CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDDATE,CREATEDBY,WORKORDERFRAME_ID,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP)select objective_guid,'USZP59','ORG016','NORMAL',sysdate,'"+isvip+"','OPEN',CREATEDDATE,CREATEDBY,map_id,'OPEN',map_id,'2B3TK2' from Ob_visitTask where visittask_guid='"+relist[j].get("vguid")+"'"
					def updatesqlresult45=eliteBean.update(updatesql45,"dataSource")
					//生成 elitetask 
					def updatesql46="insert into elitetask (ELITETASK_GUID, OBJECTIVE_GUID, OBJECTIVETYPE_ID, EXPECTSTARTTIME,OBSTATUS,PRIORITY, HANDLEGROUP_ID, CREATEDBY, CREATEDDATE,CUSTOMER_GUID,CURSTEPSTATUS,CURSTEP_ID_MAP,MAP_ID,OBJECTIVESUBSTATUS,ELITETASKSTATUS, OBFLAG) select sysguid,objective_guid,'USZP59',sysdate,'OPEN','NORMAL','ORG016',CREATEDBY,CREATEDDATE,'"+isvip+"','OPEN','2B3TK2',MAP_ID,'OPEN','OPEN','1' FROM Ob_visitTask where visittask_guid='"+relist[j].get("vguid")+"'"
					def updatesqlresult46=eliteBean.update(updatesql46,"dataSource")
				}else{
					//VIP五星客户直接生成回访数据
					updatesqlresult2=eliteBean.update("insert into Ob_visitTask(VISITTASK_GUID,OBJECTIVE_GUID,BUSINESSNO,TASKSTATUS,BUSTYPE, CREATEDDATE, CREATEDBY,MAP_ID,BUSTYPE_REPORT)values('"+relist[j].get("vguid")+"','"+relist[j].get("oguid")+"','"+relist[j].get("str_M")+"',3,'"+relist[j].get("obtype")+"',to_date('"+relist[j].get("curdate")+"','yyyy-mm-dd hh24:mi:ss'),'','"+relist[j].get("mapid")+"','"+relist[j].get("btype")+"')","dataSource");				

					//生成 objective
					def updatesql45="insert into objective (OBJECTIVE_GUID, OBJECTIVETYPE_ID, HANDLEGROUP_ID,PRIORITY_ID,EXPIREDDATE,CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDDATE,CREATEDBY,WORKORDERFRAME_ID,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP)select objective_guid,'USZP59','ORG016','NORMAL',sysdate,'"+isvip+"','OPEN',CREATEDDATE,CREATEDBY,map_id,'OPEN',map_id,'2B3TK2' from Ob_visitTask where visittask_guid='"+relist[j].get("vguid")+"'";
					  
					def updatesqlresult45=eliteBean.update(updatesql45,"dataSource");
					log.debug("updatesqlresult45:"+updatesqlresult45);
					//生成 elitetask 
					def updatesql46="insert into elitetask (ELITETASK_GUID, OBJECTIVE_GUID, OBJECTIVETYPE_ID, EXPECTSTARTTIME,OBSTATUS,PRIORITY, HANDLEGROUP_ID, CREATEDBY, CREATEDDATE,CUSTOMER_GUID,CURSTEPSTATUS,CURSTEP_ID_MAP,MAP_ID,OBJECTIVESUBSTATUS,ELITETASKSTATUS, OBFLAG) select sysguid,objective_guid,'USZP59',sysdate,'OPEN','NORMAL','ORG016',CREATEDBY,CREATEDDATE,'"+isvip+"','OPEN','2B3TK2',MAP_ID,'OPEN','OPEN','1' FROM Ob_visitTask where visittask_guid='"+relist[j].get("vguid")+"'";
					def updatesqlresult46=eliteBean.update(updatesql46,"dataSource");
					log.debug("updatesqlresult46:"+updatesqlresult46);		
					
				}

			}
			
			
		}
		
			
		
		log.debug("报案已写入待回访中间表更新状态");
		def updatesql11="update OBT_BUS_LIST set TASKSTATUS=2 where TASKSTATUS=3 and  bus_type ='DDP'";
		def updatesqlresult11=eliteBean.update(updatesql11,"dataSource");
		log.debug("updatesqlresult11:"+updatesqlresult11);        



		return "1";
	}
	
	public static String doselete(String policyno,String phone,EliteBean eliteBean,String cname,String charset,int timeout,int enableSSLflag){	
		JSONObject josnbody = new JSONObject();
		josnbody.put("username","LDTP");
		josnbody.put("password","a1e9173fc8090a220c27a33b951f8f7c");
		josnbody.put("policyno",policyno);
		josnbody.put("clientname","");
		josnbody.put("mobile","");
		Map headMap=new HashMap(); //设置头文件
		headMap.put("Content-Type","application/json");
		def url="http://ecif.edhic.com/ecif/clientQueryAnswered/clientFlagToNgs";
		try{
			def msgjson = Util.doHttpPostUseHead(url,JSONObject.fromObject(josnbody).toString(),charset,timeout,enableSSLflag,headMap);
			log.debug("----客户等级查询数据--------"+phone);
			log.debug("----msgjson--------"+msgjson);
			JSONObject obj = JSONObject.fromObject(msgjson);
			def code=obj.get("code");
			def resulterror;
			if(code!= 1000){
				resulterror="2"
				return  resulterror;
			}else{
				def custClass = obj.get("data").get("custClass");
				def clientType = obj.get("data").get("clientType");
				if("VIP五星客户".equals(custClass)||"VIP五星客户-优质".equals(custClass)||"VIP五星客户-关键".equals(custClass)){
					resulterror=eliteBean.chaxun_sql("select customer_guid from(select customer_guid from CUSTOMER c  where mobile= '"+phone+"' and stringfield18 ='"+policyno+"' order by MODIFIEDDATE desc) where rownum=1","dataSource");
					if(resulterror.size()>0){
						def listtemp=resulterror[0];
						def guid = listtemp.get("customer_guid");
						eliteBean.update("update customer set fax = '"+custClass+"', homepage ='"+clientType+"' where customer_guid = '"+guid+"'","dataSource");
						return  guid;
					}else{
						def cguid=Util.getGuid();
						eliteBean.update("insert into customer(CUSTOMER_GUID,customertype_id,CUSTOMERNAME,MOBILE,createddate,createdby,MODIFIEDDATE,MODIFIEDBY,fax,homepage,stringfield18)values('"+cguid+"','KFjoin','"+cname+"','"+phone+"',sysdate,'SELITE',sysdate,'SELITE','"+custClass+"','"+clientType+"','"+policyno+"')","dataSource");
						return  cguid;
					}
				}else{
					resulterror="2"
					return  resulterror;
				}
			}
		}catch(Exception e){
			log.debug(e);
		}	
	}
}
	
	
	

